package com.masterclient.app.businessmodule.minemodule.redpack.detail;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.modules.bridge.BridgeActivity;
import com.masterclient.app.businessmodule.minemodule.redpack.adapter.RedPackedGoodsAdapter;
import com.masterclient.app.modules.coupon.adapter.RewardInviteAdapter;
import com.masterclient.app.modules.coupon.beans.RedPackedDetailsBean;
import com.masterclient.app.businessmodule.minemodule.redpack.contract.RedPackedDetailContract;
import com.masterclient.app.businessmodule.minemodule.redpack.contract.RedPackedDetailPresenter;
import com.eoner.baselibrary.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：sean
 * 创建时间：2017/8/7 15:09
 * 功能说明：
 */

public class RedPackedDetailTaskActivity extends FCBusinessActivity<RedPackedDetailPresenter> implements RedPackedDetailContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.iv_back_top)
    ImageView ivBackTop;

    private int page, type;
    private String titleType;//4: 每日购物 5 邀请好友 6 开单奖励 7 购物消费
    private RedPackedGoodsAdapter redPackedGoodsAdapter;
    private RewardInviteAdapter inviteAdapter;
    private View headView, footerView;
    private ViewHolder viewHolder;
    private List<RedPackedDetailsBean.ShMissionBean> list;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.red_task_detail_refresh;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        page = 1;
        list = new ArrayList<>();
        titleType = getBundleString("title_type");
        type = Integer.parseInt(getBundleString("type"));
        navigationBar.setTitle("0".equals(titleType) ? "收入详情" : "支出详情");
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        swipeLayout.setBackgroundResource(R.color.mainbg);
        headView = View.inflate(this, R.layout.head_redpacked, null);
        footerView = View.inflate(this, R.layout.view_foot_text, null);
        footerView.setVisibility(View.GONE);
        viewHolder = new ViewHolder(headView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.top = parent.getChildAdapterPosition(view) == 0 ? 0 : ScreenUtils.dp2px(10);
            }
        });

        switch (type) {
            case 5:
                inviteAdapter = new RewardInviteAdapter();
                inviteAdapter.setHeaderView(headView);
                inviteAdapter.setFooterView(footerView);
                recyclerView.setAdapter(inviteAdapter);
                inviteAdapter.bindToRecyclerView(recyclerView);

                inviteAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                    @Override
                    public void onLoadMoreRequested() {
                        ++page;
                        mPresenter.getRedPackedOrderDetail(getBundleString("id"), page);
                    }
                });
                break;
            default:
                redPackedGoodsAdapter = new RedPackedGoodsAdapter(type);
                redPackedGoodsAdapter.setHeaderView(headView);
                recyclerView.setAdapter(redPackedGoodsAdapter);
                redPackedGoodsAdapter.bindToRecyclerView(recyclerView);
                redPackedGoodsAdapter.setHeaderView(headView);
                redPackedGoodsAdapter.setFooterView(footerView);
                redPackedGoodsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
                    @Override
                    public void onLoadMoreRequested() {
                        ++page;
                        mPresenter.getRedPackedOrderDetail(getBundleString("id"), page);
                    }
                });
        }

        viewHolder.headRedpackedDesc.setText("0".equals(titleType) ? "获取数量（币）" : "支出数量（币）");
        mPresenter.getRedPackedOrderDetail(getBundleString("id"), page);

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                page = 1;
                mPresenter.getRedPackedOrderDetail(getBundleString("id"), page);
            }
        });
    }

    @Override
    public void getDetailSuccess(RedPackedDetailsBean dataBean) {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
        if (dataBean == null) return;
        viewHolder.headRedpackedMoney.setText(("0".equals(titleType) ? "+" : "-") + dataBean.getSh_amount());

        if (dataBean.getSh_tip() != null && dataBean.getSh_tip().size() > 0 && page == 1) {
            viewHolder.headRedpackedLl.removeAllViews();
            viewHolder.headRedpackedLl.setVisibility(View.VISIBLE);
            for (RedPackedDetailsBean.ShTipBean bean : dataBean.getSh_tip()) {
                View item = View.inflate(mContext, R.layout.item_cash_detail, null);
                ((TextView) item.findViewById(R.id.tv_tag)).setText(bean.getSh_title());
                TextView textView = (TextView) item.findViewById(R.id.tv_desc);
                textView.setText(bean.getSh_content());
                if (!TextUtils.isEmpty(bean.getSh_url())) {
                    textView.setTextColor(0xFF7399E5);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivitry(BridgeActivity.class, new String[][]{{"url", bean.getSh_url()}});
                        }
                    });
                }
                viewHolder.headRedpackedLl.addView(item);
            }
        }

        if (page == 1) {
            list.clear();
        }
        if (dataBean.getSh_mission().size() > 0 && dataBean.getSh_mission() != null)
            list.addAll(dataBean.getSh_mission());

        switch (type) {
            case 5:
                inviteAdapter.setNewData(list);
                break;
            default:
                redPackedGoodsAdapter.setNewData(list);
        }


        if (list.size() > 0 && dataBean.getSh_mission().size() < 15) {
            switch (type) {
                case 5:
                    inviteAdapter.setEnableLoadMore(false);
                    break;
                default:
                    redPackedGoodsAdapter.setEnableLoadMore(false);

            }
        }
    }

    @Override
    public void onClickPageStateErrorView() {
        super.onClickPageStateErrorView();
        mPresenter.getRedPackedOrderDetail(getBundleString("id"), page);
    }

    @Override
    public void getDetailFailed() {
        if (swipeLayout.isRefreshing()) {
            swipeLayout.setRefreshing(false);
        }
    }

    static
    class ViewHolder {
        @BindView(R.id.head_redpacked_icon)
        ImageView headRedpackedIcon;
        @BindView(R.id.head_redpacked_money)
        TextView headRedpackedMoney;
        @BindView(R.id.head_redpacked_desc)
        TextView headRedpackedDesc;
        @BindView(R.id.head_redpacked_ll)
        LinearLayout headRedpackedLl;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
